/*
 * Copyright (c) 2025, 2025, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
package com.oracle.svm.interpreter.ristretto.meta;

import java.util.function.Function;

import com.oracle.svm.graal.meta.SubstrateField;
import com.oracle.svm.graal.meta.SubstrateType;
import com.oracle.svm.interpreter.metadata.CremaResolvedObjectType;
import com.oracle.svm.interpreter.metadata.InterpreterResolvedJavaField;
import com.oracle.svm.interpreter.metadata.InterpreterResolvedJavaType;

import jdk.graal.compiler.debug.GraalError;
import jdk.vm.ci.meta.JavaKind;
import jdk.vm.ci.meta.JavaType;
import jdk.vm.ci.meta.ResolvedJavaField;

/**
 * JVMCI representation of a {@link ResolvedJavaField} used by Ristretto for compilation. Exists
 * once per {@link InterpreterResolvedJavaField}. Allocated during runtime compilation by a
 * {@link RistrettoConstantPool} if fields are accessed over the constant pool during parsing.
 * <p>
 * Life cycle: lives until the referencing {@link InterpreterResolvedJavaField} is gc-ed.
 */
public final class RistrettoField extends SubstrateField {
    private final InterpreterResolvedJavaField interpreterField;

    private RistrettoField(InterpreterResolvedJavaField interpreterField) {
        this.interpreterField = interpreterField;
    }

    private static final Function<InterpreterResolvedJavaField, ResolvedJavaField> RISTRETTO_FIELD_FUNCTION = RistrettoField::new;

    public static RistrettoField create(InterpreterResolvedJavaField interpreterField) {
        return (RistrettoField) interpreterField.getRistrettoField(RISTRETTO_FIELD_FUNCTION);
    }

    @Override
    public int getLocation() {
        // can re-use the offset from the interpreter field, it's derived from the dynamic hub
        return interpreterField.getOffset();
    }

    @Override
    public boolean isAccessed() {
        // In Crema, any field can potentially be accessed by dynamically loaded bytecode.
        return true;
    }

    @Override
    public boolean isReachable() {
        // In Crema, any field can potentially be accessed by dynamically loaded bytecode.
        return true;
    }

    @Override
    public boolean isWritten() {
        // In Crema, any field can potentially be accessed by dynamically loaded bytecode.
        return true;
    }

    @Override
    public int hashCode() {
        return interpreterField.hashCode();
    }

    @Override
    public SubstrateType getType() {
        JavaType fieldType = interpreterField.getType();
        if (fieldType instanceof InterpreterResolvedJavaType iType) {
            return RistrettoType.create(iType);
        }
        throw GraalError.shouldNotReachHere("Must have a ristretto type available at this point");
    }

    @Override
    public String getName() {
        return interpreterField.getName();
    }

    @Override
    public int getModifiers() {
        return interpreterField.getModifiers();
    }

    @Override
    public SubstrateType getDeclaringClass() {
        return RistrettoType.create(interpreterField.getDeclaringClass());
    }

    @Override
    public boolean isSynthetic() {
        // not implemented by svm field,
        return interpreterField.isSynthetic();
    }

    @Override
    public String toString() {
        return "RistrettoField{super=" + super.toString() + ", interpreterField=" + interpreterField + "}";
    }

    @Override
    public int getInstalledLayerNum() {
        return interpreterField.getInstalledLayerNum();
    }

    @Override
    public Object getStaticFieldBaseForRuntimeLoadedClass() {
        if (interpreterField.isStatic()) {
            CremaResolvedObjectType declaringClass = (CremaResolvedObjectType) interpreterField.getDeclaringClass();
            return declaringClass.getStaticStorage(interpreterField.getJavaKind().isPrimitive(), interpreterField.getInstalledLayerNum());
        }
        throw GraalError.shouldNotReachHere("Only static fields should end up here");
    }

    @Override
    public JavaKind getStorageKind() {
        SubstrateType fieldType = getType();
        return fieldType.getStorageKind();
    }
}
